#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/1/6 7:12
@desc
"""

from pykafka import KafkaClient

host = '192.168.100.151:9092,192.168.100.152:9092,192.168.100.153:9092'
client = KafkaClient(hosts=host)

print(client.topics)

# 消费者
topic = client.topics['test'.encode()]
consumer = topic.get_simple_consumer(consumer_group='console-consumer-5754',
                                     # 设置为False的时候不需要添加consumer_group，直接连接topic即可取到消息
                                     auto_commit_enable=True,
                                     auto_commit_interval_ms=1,
                                     # 这里就是连接多个zk
                                     # zookeeper_connect='192.168.100.151:2181,192.168.100.152:2181,192.168.100.153:2181'
                                     )

for message in consumer:
    if message is not None:
        # 打印接收到的消息体的偏移个数和值
        print(message.offset, message.value)
